#include "SimpleText.h"
#include "Transformation.h"
#include "GameObject.h"
#include "Point.h"
#include "Logger.h"
#include "Exception.h"

using namespace game_engine;
using namespace game_engine::components;
using namespace game_engine::open_gl;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

SimpleText::SimpleText(void)
    : Component(), font(), text(), hudElement(true)
{
    type = "SimpleText";
}

SimpleText::SimpleText(GameObject *obj, const FontType &f, const ColorRGB &c, 
                       const BOOL hud)
    : Component(obj), font(f, c), text(), hudElement(hud)
{
    type = "SimpleText";
}

SimpleText::~SimpleText(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void SimpleText::render(void) throw(...)
{
    // Retrive the Transformation component
    Transformation *transf;
    try
    {
        transf = dynamic_cast<Transformation*> 
            (object->getComponent("Transformation"));
    } catch (...)
    {
        string msg("[SimpleText::render] transformation component not found");
        throw RuntimeException(msg);
    }

    // If the text is part of the game's HUD
    if (hudElement)
    {
        font.drawString(transf->getPosition(), text);
    }
    // Otherwise, its position must be calculated according to current camera
    else
    {
        // Calculate offset according to camera position
        Point offset = computeCameraOffset();

        // Draw string at object's position
        font.drawString(Point(offset.getX() + transf->getXPosition(),
            offset.getY() + transf->getYPosition()), text);
    }    
}

///////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////

const string& SimpleText::getText(void) const
{
    return text;
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void SimpleText::setText(const string &txt)
{
    text = txt;
}

void SimpleText::setFontType(const open_gl::FontType &f)
{
	font.setFont(f);
}

void SimpleText::setColor(const utils::ColorRGB &c)
{
	font.setColor(c);
}
